package com.zhangzeqin.code.dao.Impl;

import com.zhangzeqin.code.dao.UserDao;
import com.zhangzeqin.code.entity.User;
import com.zhangzeqin.code.util.DruidTool;
import com.zhangzeqin.code.util.JdbcUtil;
import com.zhangzeqin.code.view.LoginHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UserDaoImpl implements UserDao {


    @Override
    public boolean register(String name, String password) {
        String sql = "insert into user (name,passWord) values(?,?);";
        //连接数据库
        int i=0;
        try {
            i = JdbcUtil.executeUpdate(sql, name, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean landing(String name, String password) {
        String sql = "select * from user where name=? and passWord=?;";
        //连接数据库
        boolean flag=false ;
        try {
            flag = JdbcUtil.Query(sql, name, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }



    @Override
    public boolean completeUser(String nickName,String gender,String birth,String number,String password){
        int row=0;
        if(password.length()==0){
            String sql="update user set nickName=? , gender=?,birth=? , number=? where name='"
                    +LoginHandler.user+"';";
            //连接数据库
            try {
                row=JdbcUtil.executeUpdate(sql,nickName,gender,birth,number);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        else{
            String sql="update user set nickName=? , gender=?,birth=? , number=? , passWord=?" +
                    "where name="+LoginHandler.user+";";
            //连接数据库
            try {
                row=JdbcUtil.executeUpdate(sql,nickName,gender,birth,number,password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (row>0){
            return true;
        }else
        {
            return false;
        }
    }

    @Override
    public Object[] pInformation() throws SQLException{
        String sql="select nickName,gender,birth,number,clockDay,studyTime,wordNumber,aimWords" +
                " from user where name='"+LoginHandler.user+"';";
        Object [] arr=new Object[8];
        //获取连接
        Connection connection = DruidTool.getConnection();

        //创建预编译statement
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //返回结果集
        ResultSet resultSet = preparedStatement.executeQuery();

        /**
         * 获取结果集的元数据对象
         * 元数据对象中该结果一共有几列,列名等信息
         */
        ResultSetMetaData metaData = resultSet.getMetaData();

        //获取结果集列数
        int columnCount = metaData.getColumnCount();
        //把结果集的该条记录的每一个单元格的值取出来,存入数组中
        while(resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                arr[i - 1] = resultSet.getObject(i);
            }
        }
        if (resultSet.next()){
            //是否收回连接,需要考虑是不是事务
            if(connection.getAutoCommit()){
                //没有开启事务,正常回收连接
                DruidTool.recycleConnection(connection);
            }
            resultSet.close();

        }

        return arr;
    }

    @Override
    public Object[][] rankUser() {
        String sql="select nickName,wordNumber from user order by wordNumber desc;";
        Object [][] arr = new Object[100][2];
        //作为数组第一维的索引
        int i=0;
        List<User> list=null;
        try {
            list=JdbcUtil.selectAll(User.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            User user= (User) iterator.next();
            //给二维数组每个i赋值
            getArr(arr, i, user);
            i++;
        }
        return arr;
    }

    @Override
    public boolean modifyPlan(String aimWord) {
        String sql="update user set aimWords=? where name='"+LoginHandler.user+"';";
        int row=0;
        try {
            row = JdbcUtil.executeUpdate(sql,aimWord);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if(row!=0){
            return true;
        }
        return false;
    }

    private void getArr(Object[][] arr, int i, User user) {
        arr[i][0] = user.getNickName();
        arr[i][1] = user.getWordNumber();
    }
}
